package com.fand.platform.userCenter.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import com.fand.platform.userCenter.dao.DepartmentDao;
import com.fand.platform.userCenter.dto.DepartmentDto;
import com.fand.platform.userCenter.dto.DepartmentLaDto;
import com.fand.platform.userCenter.dto.DepartmentReDto;

/**
 * 部门服务
 *
 * @author yaoHT
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class DepartmentDefaultService implements DepartmentService {
	private DepartmentDao departmentDao;
	
	@Override
	@CacheEvict(value = DepartmentService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	@Transactional
	public DepartmentDto saveDepartmentByIdOrCodeForDepartment(DepartmentDto saveDepartment) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDepartment.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DepartmentDto oldDepartment = this.departmentDao.selectDepartmentBy1ForMappedObject(DepartmentDto.class,
				saveDepartment, null, whereEqProperties);

		if (oldDepartment == null) {
			this.departmentDao.insertDepartmentBy1(saveDepartment);

			return saveDepartment;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDepartment, oldDepartment, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.departmentDao.updateDepartmentBy1(saveDepartment, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDepartment, oldDepartment, updateProperties);
		}

		return oldDepartment;
	}

	@Override
	@Transactional
	public DepartmentDto saveDepartmentBy2ForDepartment(DepartmentDto saveDepartment) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDepartment.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DepartmentDto oldDepartment = this.departmentDao.selectDepartmentBy1ForMappedObject(DepartmentDto.class,
				saveDepartment, null, whereEqProperties);

		if (oldDepartment == null) {
			this.departmentDao.insertDepartmentBy1(saveDepartment, new String[] {});

			return saveDepartment;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDepartment, oldDepartment, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.departmentDao.updateDepartmentBy1(saveDepartment, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDepartment, oldDepartment, updateProperties);
		}

		return oldDepartment;
	}

	@Override
	@Transactional
	public int deleteDepartmentBy1(DepartmentDto deleteDepartment) {
		return this.departmentDao.deleteDepartmentBy1(deleteDepartment, new String[] {});
	}

	@Override
	@Transactional
	public int deleteDepartmentByIds(DepartmentDto deleteDepartment) {
		return this.departmentDao.deleteDepartmentByIds(deleteDepartment);
	}

	@Override
	@Transactional
	public int setDepartmentBy1(DepartmentDto setDepartment) {
		return this.departmentDao.updateDepartmentBy1(setDepartment, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setDepartmentBy2(DepartmentDto setDepartment) {
		return this.departmentDao.updateDepartmentBy2(setDepartment);
	}

	@Override
	public Integer readDepartmentByCodeNqIdForInteger(DepartmentDto readDepartment) {
		return this.departmentDao.selectDepartmentByCodeNqIdForInteger(readDepartment);
	}

	@Override
	public Integer readDepartmentBy2ForInteger(DepartmentDto readDepartment) {
		return this.departmentDao.selectDepartmentBy2ForObject(Integer.class, readDepartment, null);
	}

	@Override
	public List<Integer> readListDepartmentBy1(DepartmentDto readDepartment) {
		return this.departmentDao.selectDepartmentBy1ForObjectList(Integer.class, readDepartment, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListDepartmentBy2(DepartmentDto readDepartment) {
		return this.departmentDao.selectDepartmentBy2ForObjectList(Integer.class, readDepartment, null);
	}

	@Override
	public DepartmentDto getDepartmentBy1(DepartmentDto getDepartment) {
		return this.departmentDao.selectDepartmentBy1ForMappedObject(DepartmentDto.class, getDepartment, null, "id");
	}

	@Override
	public DepartmentDto getDepartmentById(DepartmentDto getDepartment) {
		return this.departmentDao.selectDepartmentBy1ForMappedObject(DepartmentDto.class, getDepartment, null, "id");
	}

	@Override
	public List<DepartmentDto> findDepartmentBy1(DepartmentDto findDepartment) {
		return this.departmentDao.selectDepartmentBy1ForMappedObjectList(DepartmentDto.class, findDepartment,
				new String[] {}, new String[] {});
	}

	@Override
	public List<DepartmentDto> findDepartmentBy2(DepartmentDto findDepartment) {
		return this.departmentDao.selectDepartmentBy2ForMappedObjectList(DepartmentDto.class, findDepartment);
	}

	@Override
	public PaginationDto<DepartmentDto> paginationDepartmentBy1ForDepartmentList(DepartmentDto paginationDepartment) {
		return this.departmentDao.paginationDepartmentBy1ForMappedObjectList(DepartmentDto.class,
				paginationDepartment);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationDepartmentBy1ForMapList(DepartmentDto paginationDepartment) {
		return this.departmentDao.paginationDepartmentBy1ForMapList(paginationDepartment);
	}

	@Override
	@Transactional
	public DepartmentLaDto saveDepartmentLaByIdOrCodeForDepartmentLa(DepartmentLaDto saveDepartmentLa) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDepartmentLa.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DepartmentLaDto oldDepartmentLa = this.departmentDao.selectDepartmentLaBy1ForMappedObject(DepartmentLaDto.class,
				saveDepartmentLa, null, whereEqProperties);

		if (oldDepartmentLa == null) {
			this.departmentDao.insertDepartmentLaBy1(saveDepartmentLa);

			return saveDepartmentLa;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDepartmentLa, oldDepartmentLa, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.departmentDao.updateDepartmentLaBy1(saveDepartmentLa, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDepartmentLa, oldDepartmentLa, updateProperties);
		}

		return oldDepartmentLa;
	}

	@Override
	@Transactional
	public DepartmentLaDto saveDepartmentLaBy2ForDepartmentLa(DepartmentLaDto saveDepartmentLa) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDepartmentLa.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DepartmentLaDto oldDepartmentLa = this.departmentDao.selectDepartmentLaBy1ForMappedObject(DepartmentLaDto.class,
				saveDepartmentLa, null, whereEqProperties);

		if (oldDepartmentLa == null) {
			this.departmentDao.insertDepartmentLaBy1(saveDepartmentLa, new String[] {});

			return saveDepartmentLa;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDepartmentLa, oldDepartmentLa, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.departmentDao.updateDepartmentLaBy1(saveDepartmentLa, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDepartmentLa, oldDepartmentLa, updateProperties);
		}

		return oldDepartmentLa;
	}

	@Override
	@Transactional
	public int deleteDepartmentLaBy1(DepartmentLaDto deleteDepartmentLa) {
		return this.departmentDao.deleteDepartmentLaBy1(deleteDepartmentLa, new String[] {});
	}

	@Override
	@Transactional
	public int deleteDepartmentLaByIds(DepartmentLaDto deleteDepartmentLa) {
		return this.departmentDao.deleteDepartmentLaByIds(deleteDepartmentLa);
	}

	@Override
	@Transactional
	public int setDepartmentLaBy1(DepartmentLaDto setDepartmentLa) {
		return this.departmentDao.updateDepartmentLaBy1(setDepartmentLa, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setDepartmentLaBy2(DepartmentLaDto setDepartmentLa) {
		return this.departmentDao.updateDepartmentLaBy2(setDepartmentLa);
	}

	@Override
	public Integer readDepartmentLaByCodeNqIdForInteger(DepartmentLaDto readDepartmentLa) {
		return this.departmentDao.selectDepartmentLaByCodeNqIdForInteger(readDepartmentLa);
	}

	@Override
	public Integer readDepartmentLaBy2ForInteger(DepartmentLaDto readDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy2ForObject(Integer.class, readDepartmentLa, null);
	}

	@Override
	public List<Integer> readListDepartmentLaBy1(DepartmentLaDto readDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy1ForObjectList(Integer.class, readDepartmentLa, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListDepartmentLaBy2(DepartmentLaDto readDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy2ForObjectList(Integer.class, readDepartmentLa, null);
	}

	@Override
	public DepartmentLaDto getDepartmentLaBy1(DepartmentLaDto getDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy1ForMappedObject(DepartmentLaDto.class, getDepartmentLa, null, "id");
	}

	@Override
	public DepartmentLaDto getDepartmentLaById(DepartmentLaDto getDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy1ForMappedObject(DepartmentLaDto.class, getDepartmentLa, null, "id");
	}

	@Override
	public List<DepartmentLaDto> findDepartmentLaBy1(DepartmentLaDto findDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy1ForMappedObjectList(DepartmentLaDto.class, findDepartmentLa,
				new String[] {}, new String[] {});
	}

	@Override
	public List<DepartmentLaDto> findDepartmentLaBy2(DepartmentLaDto findDepartmentLa) {
		return this.departmentDao.selectDepartmentLaBy2ForMappedObjectList(DepartmentLaDto.class, findDepartmentLa);
	}

	@Override
	public PaginationDto<DepartmentLaDto> paginationDepartmentLaBy1ForDepartmentLaList(DepartmentLaDto paginationDepartmentLa) {
		return this.departmentDao.paginationDepartmentLaBy1ForMappedObjectList(DepartmentLaDto.class,
				paginationDepartmentLa);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationDepartmentLaBy1ForMapList(DepartmentLaDto paginationDepartmentLa) {
		return this.departmentDao.paginationDepartmentLaBy1ForMapList(paginationDepartmentLa);
	}

	@Override
	@Transactional
	public DepartmentReDto saveDepartmentReByIdOrCodeForDepartmentRe(DepartmentReDto saveDepartmentRe) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDepartmentRe.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DepartmentReDto oldDepartmentRe = this.departmentDao.selectDepartmentReBy1ForMappedObject(DepartmentReDto.class,
				saveDepartmentRe, null, whereEqProperties);

		if (oldDepartmentRe == null) {
			this.departmentDao.insertDepartmentReBy1(saveDepartmentRe);

			return saveDepartmentRe;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDepartmentRe, oldDepartmentRe, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.departmentDao.updateDepartmentReBy1(saveDepartmentRe, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDepartmentRe, oldDepartmentRe, updateProperties);
		}

		return oldDepartmentRe;
	}

	@Override
	@Transactional
	public DepartmentReDto saveDepartmentReBy2ForDepartmentRe(DepartmentReDto saveDepartmentRe) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDepartmentRe.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DepartmentReDto oldDepartmentRe = this.departmentDao.selectDepartmentReBy1ForMappedObject(DepartmentReDto.class,
				saveDepartmentRe, null, whereEqProperties);

		if (oldDepartmentRe == null) {
			this.departmentDao.insertDepartmentReBy1(saveDepartmentRe, new String[] {});

			return saveDepartmentRe;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDepartmentRe, oldDepartmentRe, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.departmentDao.updateDepartmentReBy1(saveDepartmentRe, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDepartmentRe, oldDepartmentRe, updateProperties);
		}

		return oldDepartmentRe;
	}

	@Override
	@Transactional
	public int deleteDepartmentReBy1(DepartmentReDto deleteDepartmentRe) {
		return this.departmentDao.deleteDepartmentReBy1(deleteDepartmentRe, new String[] {});
	}

	@Override
	@Transactional
	public int deleteDepartmentReByIds(DepartmentReDto deleteDepartmentRe) {
		return this.departmentDao.deleteDepartmentReByIds(deleteDepartmentRe);
	}

	@Override
	@Transactional
	public int setDepartmentReBy1(DepartmentReDto setDepartmentRe) {
		return this.departmentDao.updateDepartmentReBy1(setDepartmentRe, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setDepartmentReBy2(DepartmentReDto setDepartmentRe) {
		return this.departmentDao.updateDepartmentReBy2(setDepartmentRe);
	}

	@Override
	public Integer readDepartmentReByCodeNqIdForInteger(DepartmentReDto readDepartmentRe) {
		return this.departmentDao.selectDepartmentReByCodeNqIdForInteger(readDepartmentRe);
	}

	@Override
	public Integer readDepartmentReBy2ForInteger(DepartmentReDto readDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy2ForObject(Integer.class, readDepartmentRe, null);
	}

	@Override
	public List<Integer> readListDepartmentReBy1(DepartmentReDto readDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy1ForObjectList(Integer.class, readDepartmentRe, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListDepartmentReBy2(DepartmentReDto readDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy2ForObjectList(Integer.class, readDepartmentRe, null);
	}

	@Override
	public DepartmentReDto getDepartmentReBy1(DepartmentReDto getDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy1ForMappedObject(DepartmentReDto.class, getDepartmentRe, null, "id");
	}

	@Override
	public DepartmentReDto getDepartmentReById(DepartmentReDto getDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy1ForMappedObject(DepartmentReDto.class, getDepartmentRe, null, "id");
	}

	@Override
	public List<DepartmentReDto> findDepartmentReBy1(DepartmentReDto findDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy1ForMappedObjectList(DepartmentReDto.class, findDepartmentRe,
				new String[] {}, new String[] {});
	}

	@Override
	public List<DepartmentReDto> findDepartmentReBy2(DepartmentReDto findDepartmentRe) {
		return this.departmentDao.selectDepartmentReBy2ForMappedObjectList(DepartmentReDto.class, findDepartmentRe);
	}

	@Override
	public PaginationDto<DepartmentReDto> paginationDepartmentReBy1ForDepartmentReList(DepartmentReDto paginationDepartmentRe) {
		return this.departmentDao.paginationDepartmentReBy1ForMappedObjectList(DepartmentReDto.class,
				paginationDepartmentRe);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationDepartmentReBy1ForMapList(DepartmentReDto paginationDepartmentRe) {
		return this.departmentDao.paginationDepartmentReBy1ForMapList(paginationDepartmentRe);
	}
}